package com.heima.article.service.impl;


import com.heima.article.service.LoadArticleBehaviorService;
import com.heima.behavior.feign.BehaviorFeign;
import com.heima.common.contants.RedisConstants;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.behavior.dtos.ArticleUserInfoDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.feign.ApUserFeign;
import com.heima.utils.common.ThreadLocalUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service
public class LoadArticleBehaviorServiceImpl implements LoadArticleBehaviorService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private BehaviorFeign behaviorFeign;
    @Autowired
    private ApUserFeign userFeign;

    //加载文章行为-数据回显
    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        try {
            //创建返回参数
            //"islike":false,
            //        "isunlike":false,
            //        "iscollection":false,
            //        "isfollow":false
            Map<String,Boolean> map = new HashedMap();

            //判断是否登录
            ApUser user = (ApUser) ThreadLocalUtils.get();
            if (user == null){
                map.put("islike",false);
                map.put("isunlike",false);
                map.put("iscollection",false);
                map.put("isfollow",false);
                return ResponseResult.okResult(map);
            }

            if (dto.getArticleId() == null || dto.getAuthorId() == null){
                throw new LeadNewsException(AppHttpCodeEnum.PARAM_REQUIRE);
            }
            //创建参数去查询状态
            ArticleUserInfoDto articleUserInfoDto = new ArticleUserInfoDto();
            articleUserInfoDto.setArticleId(dto.getArticleId());
            articleUserInfoDto.setUserId(user.getId());
            articleUserInfoDto.setAuthorId(dto.getAuthorId());


            //判断是否不喜欢
            //调用feign去查看是否是否不喜欢
            //调用feign去查看是否点赞
            Map<String, Boolean> booleanMap = behaviorFeign.islikeAndisunlike(articleUserInfoDto);
            Boolean islike = booleanMap.get("islike");
            Boolean isunlike = booleanMap.get("isunlike");


            //判断是否收藏
            boolean iscollection= false;
            String iscollectionKey = RedisConstants.ARTICLE_COLLECTION+dto.getArticleId().toString();
            Object o = redisTemplate.opsForHash().get(iscollectionKey, user.getId().toString());
            if (o != null){
                iscollection=true;
            }

            //判断是否关注
            boolean isfollow = userFeign.userIsFollew(articleUserInfoDto);

            map.put("islike",islike);
            map.put("isunlike",isunlike);
            map.put("iscollection",iscollection);
            map.put("isfollow",isfollow);

            return ResponseResult.okResult(map);
        } finally {
            ThreadLocalUtils.remove();
        }
    }
}
